<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../../utils/vue.js"></script>
</head>

<body>
        <p id="app">
            <!-- keep-alive标签可在组件切换时保持组件的状态，避免反复重渲染导致的性能问题 
                keep-alive要求切换的组件要有自己的名字，不论通过组件的name选项还是局部/全局注册
                
                未使用keep-alive标签Vue将会重新创建一个新的currentView实例

            -->
            <keep-alive>
                <component :is="currentView1"></component>
            </keep-alive>
            <button @click="changeView('A')">切换到A</button>
            <button @click="changeView('B')">切换到B</button>
            <button @click="changeView('C')">切换到C</button>
        </p>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                currentView1: 'comA'
            },
            methods: {
                changeView: function (data) {
                    this.currentView1 = 'com' + data //动态地改变currentView的值就可以动态挂载组件了。
                }
            },
            components: {
                comA: {
                    template: `
                        <div>
                            <input type="checkbox" name="cb" value="cb1">cb1
                            <input type="checkbox" name="cb" value="cb2">cb2
                            <input type="checkbox" name="cb" value="cb3">cb3
                            <input type="checkbox" name="cb" value="cb4">cb4
                            <input type="checkbox" name="cb" value="cb5">cb5
                        </div>
                    `
                },
                comB: {
                    template: '<p>组件B</p>'
                },
                comC: {
                    template: '<p>组件C</p>'
                }
            }
        });
    </script>
</body>

</html>